<?php
namespace Admin\Controller;
use Think\Controller;
class SiteController extends CommonController {
	public function index()
	{
		$this->_page("Site",10,$order="id");
		$this->display();
	}

//logo上传
	public function upload(){  
		if (I('n')==8) {
			$path="Logo/";
			$saveName="Logo";
		}else{
			$path="Weixin/";
			$saveName="Weixin";
		}
		$upload = new \Think\Upload();// 实例化上传类    
		$upload->autoSub = false;
		$upload->maxSize   =     3145728 ;// 设置附件上传大小    
		$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型    
		$upload->savePath  =      $path; // 设置附件上传目录    // 上传文件   
		$upload->saveName = $saveName;
		$upload->replace   =     true;
		$info   =   $upload->upload(); 
 
		 $data['title']="Uploads/".$info['photo']['savepath'].$info['photo']['savename'];
		 $site=M('Site');
		 $ph=$site->field('title')->where("id=".I("n"))->find();
		 if (!empty($ph)) {
		 	 $list=$site->where('id='.I("n"))->save($data);
		 }else{
		 	 $list=$site->where('id='.I("n"))->add($data);
		 }

		if(!$info&&$list) {
			// 上传错误提示错误信息        
			$this->error($upload->getError());   
			 }else{
			 // 上传成功        
			    
			       echo "<script>
			               alert('成功');
			               location.href='/Admin/Site/index';//客户端跳转
			             </script>";   
		}
   
	

	}

		public function update()
		{
		$data['id']=I('type');
        $data['title']=I('val');  
        $db=M("Site");
            $list = $db->save($data);
            if ($list !== false) {
               echo '数据更新成功！';
            } else {
              echo "没有更新任何数据!";
            }
		}
		//修改状态
	public function edit()
	{
		$data['id']=I("post.sid");
		$data['status']=I("post.sta");
		if (!empty($data)) {
			$Site=M("Site");
			$list=$Site->save($data);
			if ($list!=false) {
				$this->ajaxReturn('修改成功');
				
			}else{
				$this->ajaxReturn('修改失败');
			}
		}  
	}
	//flash管理
	public function flash()
	{
		$F=D("Flash");
		$Farr=$F->order("sort asc")->select();
		$this->Farr=$Farr;
		$this->display();
	}


		//修改状态
	public function status()
	{   
		$data['fid']=I("post.sid");
		$data['status']=I("post.sta");
		if (!empty($data)) {
			$Site=M("Flash");
			$list=$Site->save($data);
			if ($list!=false) {
				$this->ajaxReturn('修改成功');
				
			}else{
				$this->ajaxReturn('修改失败');
			}
		}  
	}

//修改排序
	public function fedit()
	{   
		$data['fid']=I("post.type");
		$data['sort']=I("post.val");
		if (!empty($data)) {
			$Site=M("Flash");
			$list=$Site->save($data);
			if ($list!=false) {
				$this->ajaxReturn('修改成功');
				
			}else{
				$this->ajaxReturn('修改失败');
			}
		}  
	}
	//Flash删除
	public function delete()
	{
		$sid=I("post.sid");
		$F=M('Flash');
		$file=$F->field("psrc")->where('fid='.$sid)->find();
		$A="/".$file['psrc'];
		@unlink($A);

		$name="Flash";
		$this->_foreverdelete($name,$sid,'fid');
	}
	//flash编辑
	public function fupdate()
	{
		$name="Flash";
		if (!empty($_POST)) {
		$this->_update($name);
		}else{
		$this->_edit($name);
		}
	}

	public function fupload()
	{
					
		if (!empty($_POST['n'])) { 
			$path="Flash/";
			$F=M("Flash");
			$Farr=$F->field("psrc")->where('fid='.I("post.n"))->find();
			$str=explode("/",$Farr['psrc']);
		    $savename=$str['2'];
			$upload = new \Think\Upload();// 实例化上传类    
			$upload->autoSub = false;
			$upload->maxSize   =     3145728 ;// 设置附件上传大小    
			$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型    
			$upload->savePath  =      $path; // 设置附件上传目录    // 上传文件   
			$upload->saveName =  $savename;
			$upload->replace   =     true;
			$info   =   $upload->upload(); 
		 $data['psrc']="Uploads/".$info['photo']['savepath'].$info['photo']['savename'];
		 if (!empty($Farr)) {
		 	 $list=$F->where('fid='.I("n"))->save($data);
		 }else{
		 	 $list=$F->where('fid='.I("n"))->add($data);
		 }
		if(!$info&&$list) {
			// 上传错误提示错误信息        
			$this->error($upload->getError());   
			 }else{
			 // 上传成功        
			       echo "<script>
			               alert('成功');
			               history.go(-1);
			             </script>";   
		}   
		}
	}
	//Flash新增
	public function finsert()
	{
		$F=D("Flash");
		if (!empty($_POST)) {	   
			$rules = array (    
			 array('create_time','time',1,'function'), // 对update_time字段在更新的时候写入当前时间戳
			 );
			 if ($F->auto($rules)->create()) {
			 	echo $list=$F->add();
			 	$path="Flash/";
			 	if ($list) {
			 		    $upload = new \Think\Upload();// 实例化上传类    
						$upload->autoSub = false;
						$upload->maxSize   =     3145728 ;// 设置附件上传大小    
						$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型    
						$upload->savePath  =      $path; // 设置附件上传目录    // 上传文件   
						$upload->saveName =  time().rand(); 
						$upload->replace   =     true;
						$info   =   $upload->upload(); 
					    $data['psrc']="Uploads/".$info['photo']['savepath'].$info['photo']['savename'];
				 	    $ulist=$F->where('fid='.$list)->save($data);
				 	   if(!$info&&$ulist) {
					// 上传错误提示错误信息        
					   $this->error($upload->getError());   
					 }else{
					 // 上传成功        
					  $this->success('成功','/Admin/Site/flash');
					}  

			 	}else{
			 		$this->error($F->getError());
			 	}
			 }	 		
		}else{
			$this->display();
		}
		
	}

}